package com.tg.app.activity.device.settings;

import static com.tg.app.activity.device.DeviceSettingsActivity.ACTION_DEVICE_SETTINGS;
import static com.tg.app.activity.device.DeviceSettingsActivity.EXT_DEVICE_INFO;
import static com.tg.app.activity.device.DeviceSettingsActivity.NIGHT_VISION_ACTIVITY;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import com.appbase.custom.constant.CommonConstants;
import com.appbase.custom.constant.DoubleLightModeConstants;
import com.appbase.custom.constant.NightVisionModeConstants;
import com.demo.view.SettingItemTextView;
import com.tange.module.device.feature.DeviceFeature;
import com.tange.module.device.feature.DeviceFeatureObjectBox;
import com.tange.module.device.feature.DeviceFeature_;
import com.tg.app.R;
import com.tg.app.activity.base.DeviceSettingsBaseActivity;
import com.tg.app.activity.device.DeviceSettingHelper;
import com.tg.app.activity.device.DeviceSettingsActivity;
import com.tg.app.camera.Camera;
import com.tg.app.util.LogUtils;
import com.tg.data.bean.DeviceItem;
import com.tg.data.bean.DeviceSettingsInfo;

import io.objectbox.Box;

public class LightSettingsActivity extends DeviceSettingsBaseActivity {
    private DeviceSettingsInfo mDeviceSettingsInfo;
    private DeviceSettingHelper.DeviceSettingsBroadcastReceiver receiver;
    private DeviceItem mDevice;
    private SettingItemTextView mDoubleLight, mNightMode;
    @Override
    protected void onSetupFailed() {}

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        hideActionBar();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_light_settings);
        initView();
        mDevice = getIntent().getParcelableExtra(CommonConstants.EXT_DEVICE_ITEM);
        mDeviceSettingsInfo = getIntent().getParcelableExtra(DeviceSettingsActivity.EXT_DEVICE_INFO);
        if (mDevice!=null){
            Box<DeviceFeature> box = DeviceFeatureObjectBox.getDeviceFeature();
            if (box != null){
                DeviceFeature  mDeviceFeature = box.query().equal(DeviceFeature_.uuid, mDevice.uuid).build().findFirst();
                if (mDeviceFeature!=null){
                    if (!mDeviceFeature.doubleLight){
                        mDoubleLight.setVisibility(View.GONE);
                    }
                    if (!mDeviceFeature.dayNight){
                        mNightMode.setVisibility(View.GONE);
                        mDoubleLight.showLine(false);
                    }
                }
            }
        }
        setData();
        registerReceiver();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setNightVisionText();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    private void registerReceiver() {
        receiver = DeviceSettingHelper.registerReceiver(this, (context, intent) -> {
            if (ACTION_DEVICE_SETTINGS.equals(intent.getAction())) {
                DeviceSettingsInfo rInfo = intent.getParcelableExtra(EXT_DEVICE_INFO);
                if (rInfo != null) {
                    if (mDeviceSettingsInfo != null) {
                        rInfo.id = mDeviceSettingsInfo.id;
                    }
                    mDeviceSettingsInfo = rInfo;
                    DeviceSettingHelper.updateSettings(null, mDeviceSettingsInfo);
                    setNightVisionText();
                }
            }
        });
    }

    private void setNightVisionText() {
        @StringRes int resId = R.string.settings_always_night_mode;
        if (mDeviceSettingsInfo != null && mDeviceSettingsInfo.nightVision == NightVisionModeConstants.NIGHT_VISION_MODE_AUTO) {
            resId = R.string.settings_auto_switch_night_mode;
        } else if (mDeviceSettingsInfo != null && mDeviceSettingsInfo.nightVision == NightVisionModeConstants.NIGHT_VISION_MODE_DAY) {
            resId = R.string.settings_close_night_mode;
        }
        mNightMode.setContent(resId);
    }


    @Override
    protected void initView() {
        modifyToolBar(R.string.settings_light_settings);
        mNightMode = findViewById(R.id.rl_settings_night_vision);
        mDoubleLight = findViewById(R.id.rl_settings_double_light);
        setListener();
        backClickEvent();
    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    private void setListener() {
        mDoubleLight.setOnClickListener(v -> {
            Intent intent = new Intent(getBaseContext(),DoubleLightActivity.class);
            intent.putExtra(EXT_DEVICE_INFO, mDeviceSettingsInfo);
            intent.putExtra(Camera.EXT_UUID, getIntent().getStringExtra(Camera.EXT_UUID));
            intent.putExtra(CommonConstants.EXT_DEVICE_ITEM, mDevice);
            startActivityForResult(intent,0);
        });

        mNightMode.setOnClickListener(v -> {
            DeviceSettingHelper.doJump(LightSettingsActivity.this, NIGHT_VISION_ACTIVITY, mDeviceSettingsInfo, mDevice);
            LogUtils.onEventClickByName("device_settings", "night_vision");
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK){
            mDeviceSettingsInfo  = data.getParcelableExtra(DeviceSettingsActivity.EXT_DEVICE_INFO);
            setData();
        }
    }


    private void setData(){
        @StringRes int resId = 0;
        if (mDeviceSettingsInfo != null && mDeviceSettingsInfo.doubleLight == DoubleLightModeConstants.DOUBLE_LIGHT_MODE_ON) {
            resId = R.string.open_alltime;
        } else if (mDeviceSettingsInfo != null && mDeviceSettingsInfo.doubleLight == DoubleLightModeConstants.DOUBLE_LIGHT_MODE_OFF) {
            resId = R.string.always_off;
        } else if (mDeviceSettingsInfo != null && mDeviceSettingsInfo.doubleLight == DoubleLightModeConstants.DOUBLE_LIGHT_MODE_AUTO) {
            resId = R.string.auto;
        } else if (mDeviceSettingsInfo != null && mDeviceSettingsInfo.doubleLight == DoubleLightModeConstants.DOUBLE_LIGHT_MODE_TIMER){
            resId = R.string.settings_open_regularly;
        }
        mDoubleLight.setContent(resId);
    }
}